<template>
  <div class="app-container">
    <ContentWrap>
      <el-row :gutter="20">
        <el-col :span="4">
          <RegionTree node-type="2" :node-click="handleDmaNodeClick"/>
        </el-col>

        <el-col :span="20" class="content-container">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                   label-width="68px">
            <el-form-item label="报表类型" prop="frozenType">
              <el-radio-group v-model="queryParams.frozenType" @change="handleFrozenTypeChange">
                <el-radio label="day">日</el-radio>
                <el-radio label="month">月</el-radio>
                <!--              <el-radio label="year">年</el-radio>-->
              </el-radio-group>
            </el-form-item>
            <el-form-item label="开始日期" prop="startDate">
              <el-date-picker v-model="queryParams.startDate" :type="dateType" :value-format="valueType"
                              placeholder="选择日期"></el-date-picker>
            </el-form-item>
            <el-form-item label="结束日期" prop="endDate">
              <el-date-picker v-model="queryParams.endDate" :type="dateType" :value-format="valueType"
                              placeholder="选择日期"></el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>

          <el-table v-loading="loading" :data="freezeDfqList" border :height="getAvailableHeight">
            <el-table-column type="index" width="40" align="center" label="#" :fixed="true"/>
            <!--      <el-table-column type="selection" width="55" align="center"/>-->
            <!--      <el-table-column label="设备归属租户" align="center" prop="tenantId"/>-->
            <!--      <el-table-column label="层级id" align="center" prop="areaid"/>-->
            <el-table-column label="名称" align="center" prop="zone.areaName"/>
            <el-table-column label="日期" align="center" prop="freezetime" width="100"/>
            <!--      <el-table-column label="冻结类型，日：day，月：month" align="center" prop="frozenType"/>-->
            <el-table-column label="供水量(m³)" align="center" prop="inputFlow" width="120" sortable/>
            <el-table-column label="售水量(m³)" align="center" prop="saleWater" width="120" sortable/>
            <el-table-column label="无收益水量(m³)" align="center" prop="freeWater" width="150" sortable/>
            <el-table-column label="背景漏失(m³/h)" align="center" prop="uarl" width="150" sortable/>
            <el-table-column v-if="queryParams.frozenType==='day'" label="物理漏失(m³/天)" align="center"
                             prop="physicalLeakageDaily" width="150" sortable/>
            <el-table-column v-if="queryParams.frozenType==='month'" label="物理漏失(m³/月)" align="center"
                             prop="physicalLeakageMonth" width="150" sortable/>
            <el-table-column label="漏损率(%)" align="center" width="120" prop="lsl" sortable>
              <template slot-scope="scope">
                <span :style="getLslStyle(scope.row.lsl)"> {{ scope.row.lsl }}</span>
              </template>
            </el-table-column>
            <el-table-column label="产销差率(%)" align="center" width="120" prop="cxcl" sortable>
              <template slot-scope="scope">
                <span :style="getLslStyle(scope.row.cxcl)"> {{ scope.row.cxcl }}</span>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            :total="total"
            :page-sizes="[15, 30, 45, 60]"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-col>
      </el-row>
    </ContentWrap>
  </div>
</template>

<script>
import {listByQueryModel} from "@/api/leakage/freezeDma";
import RegionTree from "@/views/leakage/components/RegionTree.vue";

export default {
  name: "FreezeDma",
  components: {RegionTree},
  data() {
    return {
      screenHeight: null,
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // DMA分区冻结数据表格数据
      freezeDfqList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        areaId: null,
        frozenType: 'day',
        startDate: '',
        endDate: '',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      dateType: 'date',
      valueType: 'yyyy-MM-dd',
      editingZone: undefined
    };
  },
  created() {
    // this.getList();
    this.screenHeight = document.body.clientHeight
  },
  computed: {
    getAvailableHeight() {
      return this.screenHeight - 235
    },
  },
  methods: {
    /** 查询DMA分区冻结数据列表 */
    getList() {
      this.loading = true;
      if (this.editingZone) {
        this.queryParams.areaId = this.editingZone.areaId
      }
      listByQueryModel(this.queryParams).then(response => {
        this.freezeDfqList = response.rows;
        this.freezeDfqList.forEach(item => {
          let lsl
          if (this.queryParams.frozenType === 'day') {
            lsl = item.physicalLeakageDaily ? (item.physicalLeakageDaily / item.inputFlow * 100).toFixed(2) : '0'
          } else {
            lsl = item.physicalLeakageMonth ? (item.physicalLeakageMonth / item.inputFlow * 100).toFixed(2) : '0'
          }
          console.log(lsl)
          item.lsl = lsl
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('swla2/freezeDma/export', {
        ...this.queryParams
      }, `DMA分区绩效报表.xlsx`)
    },
    handleFrozenTypeChange(val) {
      if (val === 'day') {
        this.dateType = 'date'
        this.valueType = 'yyyy-MM-dd'
      } else if (val === 'month') {
        this.dateType = 'month'
        this.valueType = 'yyyy-MM'
      } else {
        this.dateType = 'year'
        this.valueType = 'yyyy'
      }
    },
    handleDmaNodeClick(node) {
      const levelType = node.levelType
      if (levelType !== '2') {
        this.editingZone = undefined
        this.$message.error('请选择DMA分区节点')
      } else {
        this.editingZone = node
        this.handleQuery()
      }
    },
    getLslStyle(lsl) {
      let color
      const fLsl = parseFloat(lsl)
      if (fLsl <= 0 || fLsl > 30) {
        color = 'red'
      } else if (fLsl > 0 && fLsl < 10) {
        color = 'green'
      } else if (fLsl > 10 && fLsl < 20) {
        color = '#ffd400'
      } else if (fLsl > 20 && fLsl < 30) {
        color = '#ff9900'
      }

      return {color: color}
    }
  }
};
</script>
